package com.xukai.springboot.controller.statistics;

import com.xukai.springboot.entity.request.statis.AddChartsRequest;
import com.xukai.springboot.entity.request.statis.DelRequest;
import com.xukai.springboot.entity.response.PageResult;
import com.xukai.springboot.entity.response.ResponseBase;
import com.xukai.springboot.entity.response.statis.ShowSceneResponse;
import com.xukai.springboot.entity.statistics.t_statis_scene;
import com.xukai.springboot.entity.statistics.t_statis_scene_chart;
import com.xukai.springboot.entity.statistics.t_statis_scene_theme;
import com.xukai.springboot.service.statistics.ChartSettingService;
import com.xukai.springboot.service.statistics.StatisSceneService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Api(basePath = "api/statis/scene", description = "场景管理")
@RestController
@RequestMapping("api/statis/scene")
public class StatisSceneController {
    /**
     * 日志
     */
    private final Logger logger = LoggerFactory.getLogger(StatisSceneController.class);

    @Autowired
    StatisSceneService statisSceneService;


    /**
     * 获取场景列表
     */
    @ApiOperation(value = "获取场景列表", notes = "获取场景列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sceneName", value = "场景名称", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "场景状态 -1：检索全部状态的场景", required = true, dataType = "int", example = "0", defaultValue = "0", paramType = "query"),
            @ApiImplicitParam(name = "start", value = "数据检索起始", required = true, dataType = "int", example = "0", defaultValue = "0", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "获取数据数据量，-1：获取全部数据", required = true, dataType = "int", example = "0", defaultValue = "0", paramType = "query")
    })
    @GetMapping("GetSceneList")
    public ResponseBase<PageResult<t_statis_scene>> GetSceneList(@RequestParam String sceneName, @RequestParam int state, @RequestParam int start, @RequestParam int pageSize) {
        ResponseBase<PageResult<t_statis_scene>> result = new ResponseBase<>();
        try {
            PageResult<t_statis_scene> resultData = new PageResult<>();
            List<t_statis_scene> data = statisSceneService.GetSceneList(sceneName, state, start, pageSize);
            int total = statisSceneService.GetSceneCount(sceneName, state);
            resultData.setResultData(data);
            resultData.setTotal(total);
            result.setData(resultData);

        } catch (Exception ex) {
            logger.error("获取场景列表", ex);
            result.setStatue(0);
        }
        return result;
    }

    /**
     * 获取场景详细信息
     */
    @ApiOperation(value = "获取场景详细信息", notes = "获取场景详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "场景ID", required = true, dataType = "int", example = "0", defaultValue = "0", paramType = "query")
    })
    @GetMapping("GetScene")
    public ResponseBase<t_statis_scene> GetScene(@RequestParam int id) {
        ResponseBase<t_statis_scene> result = new ResponseBase<>();
        try {
            t_statis_scene data = statisSceneService.GetScene(id);
            result.setData(data);
        } catch (Exception ex) {
            logger.error("获取场景详细信息", ex);
            result.setStatue(0);
        }
        return result;


    }

    /**
     * 新建场景基本信息
     */
    @ApiOperation(value = "新建场景基本信息", notes = "新建场景基本信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "data", value = "新建数据", required = true, dataType = "t_statis_scene", paramType = "body")
    })
    @PostMapping("AddSceneBase")
    public ResponseBase<t_statis_scene> AddSceneBase(@RequestBody t_statis_scene data) {
        ResponseBase<t_statis_scene> result = new ResponseBase<>();
        try {
            statisSceneService.AddSceneBase(data);
            result.setData(data);
        } catch (Exception ex) {
            logger.error("新建场景基本信息", ex);
            result.setStatue(0);
        }

        return result;
    }

    /**
     * 修改场景基本信息
     */
    @ApiOperation(value = "修改场景基本信息", notes = "修改场景基本信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "data", value = "修改数据", required = true, dataType = "t_statis_scene", paramType = "body")
    })
    @PostMapping("UpdateSceneBase")
    public ResponseBase UpdateSceneBase(@RequestBody t_statis_scene data) {
        ResponseBase result = new ResponseBase();
        try {
            statisSceneService.UpdateSceneBase(data);
        } catch (Exception ex) {
            logger.error("修改场景基本信息", ex);
            result.setStatue(0);
        }
        return result;
    }


    /**
     * 增加图表
     */
    @ApiOperation(value = "增加图表", notes = "增加图表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "data", value = "新建数据", required = true, dataType = "t_statis_scene_chart", paramType = "body")
    })
    @PostMapping("AddChart")
    public ResponseBase<t_statis_scene_chart> AddChart(@RequestBody t_statis_scene_chart chartData) {
        ResponseBase<t_statis_scene_chart> result = new ResponseBase<>();
        try {
            t_statis_scene_chart data = statisSceneService.AddChart(chartData);
            result.setData(data);
        } catch (Exception ex) {
            logger.error("增加图表", ex);
            result.setStatue(0);
        }
        return result;

    }

    /**
     * 增加图表集合
     */
    @ApiOperation(value = "增加图表集合", notes = "增加图表集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "data", value = "新建数据集合", required = true, dataType = "AddChartsRequest", paramType = "body")
    })
    @PostMapping("AddCharts")
    public ResponseBase AddCharts(@RequestBody AddChartsRequest data) {
        ResponseBase result = new ResponseBase();

        try {
            if (data.getData() == null || data.getData().size() == 0) {
                return result;
            }
            for (t_statis_scene_chart item : data.getData()) {
                //判断图表是否存在
                if (statisSceneService.ChartIsExist(item.getSceneID(), item.getChartID())) {
                    continue;
                }
                //新建图表
                statisSceneService.AddChart(item);
            }

        } catch (Exception ex) {
            logger.error("增加图表集合", ex);
            result.setStatue(0);
        }
        return result;
    }

    /**
     * 获取图表列表
     */
    @ApiOperation(value = "获取图表列表", notes = "获取图表列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sceneID", value = "场景ID", required = true, dataType = "int", example = "0", defaultValue = "0", paramType = "query"),
            @ApiImplicitParam(name = "start", value = "数据检索起始", required = true, dataType = "int", example = "0", defaultValue = "0", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "获取数据数据量，-1：获取全部数据", required = true, dataType = "int", example = "0", defaultValue = "0", paramType = "query")
    })
    @GetMapping("GetChartList")
    public ResponseBase<PageResult<t_statis_scene_chart>> GetChartList(@RequestParam int sceneID, @RequestParam int start, @RequestParam int pageSize) {
        ResponseBase<PageResult<t_statis_scene_chart>> result = new ResponseBase<>();
        try {
            PageResult<t_statis_scene_chart> resultData = new PageResult<>();
            List<t_statis_scene_chart> data = statisSceneService.GetChartList(sceneID, start, pageSize);
            int count = statisSceneService.GetChartCount(sceneID);
            resultData.setResultData(data);
            resultData.setTotal(count);
            result.setData(resultData);

        } catch (Exception ex) {
            logger.error("获取图表列表", ex);
            result.setStatue(0);
        }
        return result;
    }


    /**
     * 删除图表
     */
    @ApiOperation(value = "删除图表", notes = "删除图表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "param", value = "删除参数", required = true, dataType = "DelRequest", paramType = "body")
    })
    @PostMapping("DelChart")
    public ResponseBase DelChart(@RequestBody DelRequest param) {
        ResponseBase result = new ResponseBase();
        try {
            statisSceneService.DelChart(param.getId());
        } catch (Exception ex) {
            logger.error("删除图表", ex);
            result.setStatue(0);
        }
        return result;
    }

    /**
     * 删除场景
     */
    @ApiOperation(value = "删除场景", notes = "删除场景")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "param", value = "删除参数", required = true, dataType = "DelRequest", paramType = "body")
    })
    @PostMapping("DelScene")
    public ResponseBase DelScene(@RequestBody DelRequest param) {
        ResponseBase result = new ResponseBase();
        try {
            statisSceneService.DelScene(param.getId());
        } catch (Exception ex) {
            logger.error("删除场景", ex);
            result.setStatue(0);
        }
        return result;

    }


    /**
     * 获取主题列表
     */
    @ApiOperation(value = "获取主题列表", notes = "获取主题列表")
    @ApiImplicitParams({
    })
    @GetMapping("GetThemeList")
    public ResponseBase<List<t_statis_scene_theme>> GetThemeList() {
        ResponseBase<List<t_statis_scene_theme>> result = new ResponseBase<>();
        try {

            List<t_statis_scene_theme> data = statisSceneService.GetThemeList();
            result.setData(data);

        } catch (Exception ex) {
            logger.error("获取主题列表", ex);
            result.setStatue(0);
        }
        return result;
    }

    /**
     * 获取主题详情
     */
    @ApiOperation(value = "获取主题详情", notes = "获取主题详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主题编号", required = true, dataType = "int", example = "0", defaultValue = "0", paramType = "query")
    })
    @GetMapping("GetTheme")
    public ResponseBase<t_statis_scene_theme> GetTheme(@RequestParam int id) {
        ResponseBase<t_statis_scene_theme> result = new ResponseBase<>();
        try {

            t_statis_scene_theme data = statisSceneService.GetTheme(id);
            result.setData(data);

        } catch (Exception ex) {
            logger.error("获取主题详情", ex);
            result.setStatue(0);
        }
        return result;
    }

    /**
     * 修改排序
     */
    @ApiOperation(value = "修改排序", notes = "修改排序")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "data", value = "修改数据", required = true, dataType = "t_statis_scene_chart", paramType = "body")
    })
    @PostMapping("UpdateChartSort")
    public ResponseBase UpdateChartSort(@RequestBody t_statis_scene_chart data) {
        ResponseBase result = new ResponseBase();
        try {
            statisSceneService.UpdateChartSort(data.getSceneID(), data.getWidth(), data.getHeight(), data.getChartID(), data.getSort());

        } catch (Exception ex) {
            logger.error("增加图表", ex);
            result.setStatue(0);
        }
        return result;

    }

    /**
     * 获取展示数据
     *
     * @param sceneID
     * @return
     */
    @ApiOperation(value = "获取展示数据", notes = "获取展示数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sceneID", value = "场景编号", required = true, dataType = "int", example = "0", defaultValue = "0", paramType = "query")
    })
    @GetMapping("GetShowSceneData")
    public ResponseBase<ShowSceneResponse> GetShowSceneData(@RequestParam int sceneID) {
        ResponseBase<ShowSceneResponse> result = new ResponseBase<>();
        try {
            ShowSceneResponse resultData = new ShowSceneResponse();
            //获取基础信息
            resultData.setBaseInfo(statisSceneService.GetScene(sceneID));
            if (resultData.getBaseInfo() == null) {
                result.setStatue(1);
                result.setErrMessage("检索数据不存在");
                return result;
            }
            //获取图表
            resultData.setChartList(statisSceneService.GetChartList(resultData.getBaseInfo().getId(), 0, -1));
            //获取主题信息
            resultData.setTheme(statisSceneService.GetTheme(resultData.getBaseInfo().getThemeID()));
            result.setData(resultData);
        } catch (Exception ex) {
            logger.error("获取展示数据", ex);
            result.setStatue(0);
        }
        return result;
    }

}
